Skip to content

Fix #6702: NPE in getEndColumn for VirtualFile with newlines#6704

Open
caglareker wants to merge 2 commits intoINRIA:masterfrom
caglareker:fix/issue-6702-bug-npe-on-getendcolumn
Open

Fix #6702: NPE in getEndColumn for VirtualFile with newlines#6704
caglareker wants to merge 2 commits intoINRIA:masterfrom
caglareker:fix/issue-6702-bug-npe-on-getendcolumn

Conversation

@caglareker
Copy link
Copy Markdown

Fixes #6702

getOriginalSourceCode() returns null for VirtualFile that contains newlines, so the loop in searchColumnNumber() hits NPE. Added a null check around the source access and a small regression test using VirtualFile with newlines. Tab counting just gets skipped when source isn't available, columns will be slightly off in that case but no crash.

getOriginalSourceCode() can return null when using VirtualFile
with source containing newlines, causing NPE in getEndColumn().
CtCompilationUnit cu = launcher.getFactory().CompilationUnit().getMap().values().iterator().next();
assertTrue(cu.getPosition().isValidPosition());
// should not throw NPE
cu.getPosition().getEndColumn();
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you want to test for the absence of an exception, use e.g., assertDoesNotThrow. But also please test the expected result; if it makes sense to not throw an exception, then the result should be well-specified.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good point, switched to assertDoesNotThrow and added an assert on the returned column

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: NPE on getEndColumn

2 participants